//
//  ChannelRankListViewController.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/18.
//  Copyright © 2016年 ltq. All rights reserved.
//

// 精选排行榜页面
#import "ChannelRankListViewController.h"
#import "TAMacro.h"
#import "RankListTableViewCell.h"
#import "PersonalViewController.h"
#import "ChannelPostRanklist.h"
#import "RankListHasContentTableViewCell.h"
@interface ChannelRankListViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic, strong) UITableView *rankListTableView;
@property (nonatomic ,strong) UILabel *titleLabel;
@end

@implementation ChannelRankListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleNavigationBar];
    // Do any additional setup after loading the view.
    self.rankListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _rankListTableView.backgroundColor = [UIColor whiteColor];
    _rankListTableView.delegate = self;
    _rankListTableView.dataSource = self;
    [self.view addSubview:_rankListTableView];
//    [_rankListTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    _rankListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [_rankListTableView registerNib:[UINib nibWithNibName:@"RankListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"RankListTableViewCellReuse"];
    [_rankListTableView registerNib:[UINib nibWithNibName:@"RankListHasContentTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"RankListHasContentTableViewCellReuse"];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    backView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 20, 30)];
    sectionTitleLabel.textColor = [UIColor lightGrayColor];
    sectionTitleLabel.font = [UIFont systemFontOfSize:14];
    [backView addSubview:sectionTitleLabel];
    
    if (section == 0) {
        sectionTitleLabel.text = @"此版块的精选排行 · 前三名";
    } else {
        sectionTitleLabel.text = @"第4-10名";
    }
    return backView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    return _rankList.count - 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        if (_rankList[indexPath.row].talentDays == 0) {
            RankListHasContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankListHasContentTableViewCellReuse"];
            cell.rankList = _rankList[indexPath.row];
            return cell;
        } else {
            RankListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankListTableViewCellReuse"];
            cell.rankList = _rankList[indexPath.row];
            return cell;
        }
    } else {
        if (_rankList[indexPath.row + 3].talentDays == 0) {
            RankListHasContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankListHasContentTableViewCellReuse"];
            cell.rankList = _rankList[indexPath.row + 3];
            return cell;
        } else {
            RankListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankListTableViewCellReuse"];
            cell.rankList = _rankList[indexPath.row + 3];
            return cell;
        }

    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ChannelPostRanklist *rankList = _rankList[indexPath.row];
        PersonalViewController *personalVC = [[PersonalViewController alloc] init];
        personalVC.postId = rankList.ID;
        [self.navigationController pushViewController:personalVC animated:YES];
        
    } else {
        ChannelPostRanklist *rankList = _rankList[indexPath.row + 3];
        PersonalViewController *personalVC = [[PersonalViewController alloc] init];
        personalVC.postId = rankList.ID;
        [self.navigationController pushViewController:personalVC animated:YES];
    }
}

- (void)handleNavigationBar {
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, 42);
    _titleLabel.textAlignment = 1;
    _titleLabel.textColor = [UIColor whiteColor];
    [self.backView addSubview:_titleLabel];
    _titleLabel.text = @"精选排行榜";    
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 27, 30, 30)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.backView addSubview:backButton];
    [backButton addTarget:self action:@selector(popViewControllerAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 40, 27, 30, 30)];
    [moreButton setImage:[UIImage imageNamed:@"questionMark"] forState:UIControlStateNormal];
    [self.backView addSubview:moreButton];
    [moreButton addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

// 返回上一页
- (void)popViewControllerAction: (UIButton *)backButton {
    [self.navigationController popViewControllerAnimated:YES];
}

// 须知
- (void)moreAction:(UIButton *)moreButton {
    NSLog(@"更多");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
